﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace Qwen_for_Windows.Model
{
    public class App_Model
    {
        public static string API_Key { get; set; }
        public static string Model_Name { get; set; }
        public static string Base_URL { get; set; }

        // 定义文件路径（假设 Data 文件夹和 AppSetting.xml 文件在同一目录下）
        private static readonly string DataDirectory = "Data";
        private static readonly string fileName = "AppSetting.xml";
        private static readonly string filePath = System.IO.Path.Combine(DataDirectory, fileName);
        // 加载 XML 文件
        private static readonly XDocument appSettingXML = XDocument.Load(filePath);

        //获取 API_Key、URL、ModelName元素
        private static readonly XElement apiKeyElement = appSettingXML.Descendants("ApiKey").FirstOrDefault(e => (string)e.Attribute("name") == "API_Key");
        private static readonly XElement urlElement = appSettingXML.Descendants("Url").FirstOrDefault(e => (string)e.Attribute("name") == "Base_URL");
        private static readonly XElement modelElement = appSettingXML.Descendants("Model").FirstOrDefault(e => (string)e.Attribute("name") == "ModelName");

        //获取元素的值
        private static string apiKeyValue = (string)apiKeyElement.Attribute("value");
        private static string urlValue = (string)urlElement.Attribute("value");
        private static string modelValue = (string)modelElement.Attribute("value");

        public static void Save2XML(string API_Key, string Model_Name, string Base_URL)
        {
            apiKeyElement.SetAttributeValue("value", API_Key);
            modelElement.SetAttributeValue("value", Model_Name);
            urlElement.SetAttributeValue("value", Base_URL);
            appSettingXML.Save(filePath);
            MessageBox.Show("保存成功XML", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        public static void CheckXMLFile(Frame MainFrame)
        {
            if (!File.Exists(filePath))
            {
                MessageBox.Show("XML文件不存在");
                Qwen_File.CheckChatHistoryDirectory();
                Qwen_File.CreateXMLFile();
            }
            if (File.Exists(filePath))
            {
                try
                {
                    // 检查是否为空
                    if (string.IsNullOrEmpty(apiKeyValue) || string.IsNullOrEmpty(urlValue) || string.IsNullOrEmpty(modelValue))
                    {
                        MessageBox.Show("您是第一次打开，请到设置页面进行您的配置", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        MainFrame.Navigate(new Pages.SettingPage());
                    }
                    else
                    {
                        // 设置到Model中
                        Model.App_Model.API_Key = apiKeyValue;
                        Model.App_Model.Base_URL = urlValue;
                        Model.App_Model.Model_Name = modelValue;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"XML文件读取错误\n{ex.Message}\n请检查XML文件", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }


        public static void GetChatData(string XMLFilePath, TextBlock Title, TextBlock ModelVersion)
        {
            try
            {
                // 加载 XML 文件
                XDocument xmlDocument = XDocument.Load(XMLFilePath);

                // 安全地获取 Title 和 ModelVersion 的值
                string title = xmlDocument.Root?.Element("Title")?.Value ?? "未找到标题";
                string modelVersion = xmlDocument.Root?.Element("ModelVersion")?.Value ?? "未找到版本";

                // 将结果拼接成字符串返回（根据需求可调整格式）
                Title.Text = title;
                ModelVersion.Text = modelVersion;

            }
            catch (Exception ex)
            {
                MessageBox.Show($"XML文件读取错误\n{ex.Message}\n请检查XML文件", "提示", MessageBoxButton.OK, MessageBoxImage.Information);

            }

        }
        public static string GetChatFileTitle(string XMLFilePath)
        {
            try
            {
                // 加载 XML 文件
                XDocument xmlDocument = XDocument.Load(XMLFilePath);

                // 安全获取 Title 值（防止根节点或元素为 null）
                string title = xmlDocument.Root?.Element("Title")?.Value ?? "未找到标题";

                return title;
            }
            catch (Exception ex)
            {
                // 捕获异常并返回错误信息（可自定义错误处理逻辑）
                return $"加载文件失败: {ex.Message}";
            }
        }

    }

}
